package com.example.mytest6;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class MyFragment6 extends Fragment {
    private BarChart bar_3;

    public MyFragment6() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment6, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        bar_3.setDescription(null);
        bar_3.setScaleEnabled(false);
        bar_3.setExtraOffsets(10, 0, 10, 10);

        bar_3.getLegend().setEnabled(false);

        final List<String> xV = new ArrayList<>();
        xV.add("0:00:01-2:00:00");
        xV.add("2:00:01-4:00:00");
        xV.add("4:00:01-6:00:00");
        xV.add("6:00:01-8:00:00");
        xV.add("8:00:01-10:00:00");
        xV.add("10:00:01-12:00:00");
        xV.add("12:00:01-14:00:00");
        xV.add("14:00:01-16:00:00");
        xV.add("16:00:01-18:00:00");
        xV.add("18:00:01-20:00:00");
        xV.add("20:00:01-22:00:00");
        xV.add("22:00:01-24:00:00");


        List<BarEntry> yV1 = new ArrayList<>();
        yV1.add(new BarEntry(0, 1));
        yV1.add(new BarEntry(1, 1));
        yV1.add(new BarEntry(2, 1));
        yV1.add(new BarEntry(3, 5.5f));
        yV1.add(new BarEntry(4, 18.54f));
        yV1.add(new BarEntry(5, 21.87f));
        yV1.add(new BarEntry(6, 12));
        yV1.add(new BarEntry(7, 19.28f));
        yV1.add(new BarEntry(8, 12));
        yV1.add(new BarEntry(9, 4));
        yV1.add(new BarEntry(10, 2f));
        yV1.add(new BarEntry(11, 1f));


        BarDataSet dataSet1 = new BarDataSet(yV1, "");
        int color[] = {0xFF43658B, 0xFF2C0B14, 0xFF080280, 0xFF4F612F, 0xFFB7AED7, 0xFF7A9438, 0xFF913738, 0xFFF9C504, 0xFF4D7FB8, 0xFFE8690F, 0xFF7C295A, 0xFF662126};
        dataSet1.setColors(color);
        dataSet1.setHighlightEnabled(false);
        dataSet1.setValueTextColor(Color.RED);
        dataSet1.setValueTextSize(14);
        dataSet1.setValueFormatter(new MyData6Formatter());


        bar_3.getAxisRight().setEnabled(false);
        YAxis yAxis = bar_3.getAxisLeft();
        yAxis.setLabelCount(6, false);
        yAxis.setAxisMinimum(0);
        yAxis.setAxisMaximum(25);
        yAxis.setGranularity(1);
        yAxis.setTextSize(14);
        yAxis.setValueFormatter(new MyYAxis6Formatter());


        XAxis xAxis = bar_3.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1);
        //0开始
        xAxis.setLabelCount(12);
        //设置x轴字体显示角度
        xAxis.setLabelRotationAngle(-45);
        xAxis.setValueFormatter(new MyXAxis6Formatter(xV));


        BarData data = new BarData(dataSet1);
        data.setBarWidth(0.5f);
        bar_3.setData(data);

    }

    class MyData6Formatter extends ValueFormatter {
        @Override
        public String getBarLabel(BarEntry barEntry) {
            return new DecimalFormat("0.00%").format(barEntry.getY() / 100);
        }
    }

    class MyXAxis6Formatter extends ValueFormatter {
        List<String> xV = new ArrayList<>();

        public MyXAxis6Formatter(List<String> xV) {
            this.xV = xV;
        }

        @Override
        public String getFormattedValue(float value) {
            return xV.get((int) value);
        }
    }

    class MyYAxis6Formatter extends ValueFormatter {
        @Override
        public String getFormattedValue(float value) {
            return new DecimalFormat("0.00%").format(value / 100);
        }
    }

    private void initView(View view) {
        bar_3 = view.findViewById(R.id.bar_3);
    }

}